var results = [];

var FeedParser = require('feedparser')
  , request = require('request');

var req = request('http://discountry.github.io/feed.xml')
  , feedparser = new FeedParser();

req.on('error', function (error) {
  // handle any request errors
});
req.on('response', function (res) {
  var stream = this;

  if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));

  stream.pipe(feedparser);
});


feedparser.on('error', function(error) {
  // always handle errors
});

feedparser.on('readable', function() {
  // This is where the action is!
  var stream = this
    , meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
    , item;

  while (item = stream.read()) {
	results.push({
		title: item.title,
		description: item.date,
		picurl: 'https://mmbiz.qlogo.cn/mmbiz/DHvBCZs7W1tia4h2G7lL3x5pW3f0TnZY5RicZZ797GHH4wousCmIYtrHEZGVAhxnmiadN1nB9hgCNwrkfgJOZN6fg/0?wx_fmt=png',
		url: item.link
	});
    //console.log(results);
    
    return results;
  }
});

module.exports = results;